/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.order.app;

import com.alibaba.cloud.nacos.NacosServiceManager;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.client.env.NacosClientProperties;
import java.util.Properties;

/**
 * Nacos naming example.
 * <p>Add the JVM parameter to run the NamingExample:</p>
 * {@code -DserverAddr=${nacos.server.ip}:${nacos.server.port} -Dnamespace=${namespaceId}}
 * @author nkorange
 */
public class NamingRegisterExample1 {

    public static void main(String[] args) throws NacosException, InterruptedException {
        Properties properties = new Properties();
        properties.setProperty("serverAddr", "127.0.0.1:8848");
        properties.setProperty("namespace", "public");
        properties.setProperty("nacos.ephemeral", "false");
        NacosServiceManager nacosServiceManager = new NacosServiceManager();
        NamingService naming = nacosServiceManager.getNamingService(properties);

        Instance instance = new Instance();
        instance.setIp("11.11.11.11");
        instance.setPort(8888);
        instance.setWeight(1.0);
        //instance.setEphemeral(false);
        naming.registerInstance("nacos.test.3", instance);
        //TimeUnit.SECONDS.sleep(10);
        naming.deregisterInstance("nacos.test.3", instance);

//new ThreadPoolExecutor();
        //naming.registerInstance("nacos.test.3", "11.11.11.13", 8888);
        NacosServiceManager nacos2ServiceManager = new NacosServiceManager();
        NamingService naming2 = nacos2ServiceManager.getNamingService(properties);
        naming2.registerInstance("nacos.test.3", "11.11.11.12", 8888);
        //TimeUnit.SECONDS.sleep(2);

        System.out.println("instances after register1:" + naming.getAllInstances("nacos.test.3"));
        System.out.println("instances after register2:" + naming.selectInstances("nacos.test.3", true));
        // TimeUnit.SECONDS.sleep(60);

        // global properties
        NacosClientProperties.PROTOTYPE.setProperty("global-key1", "global-value1");
        // properties1
        NacosClientProperties properties1 = NacosClientProperties.PROTOTYPE.derive();
        properties1.setProperty("properties1-key1", "properties1-value1");
        // properties2
        NacosClientProperties properties2 = properties1.derive();
        properties2.setProperty("properties2-key1", "properties2-value1");
        System.out.println(properties2.getProperty("global-key1"));
        System.out.println(properties2.getProperty("properties2-key1"));
        System.out.println(properties1.getProperty("properties2-key1"));

    }
}
